﻿#include "contentwidget.h"
#include "ui_contentwidget.h"

ContentWidget::ContentWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ContentWidget)
{
    ui->setupUi(this);
//    ui->bookmark_widget->hide();
}

ContentWidget::~ContentWidget()
{
    delete ui;
}


/**
 * 1: 0x1fd5425a110
 * 2: 0x1fd65eba890     0x1fd65eba890
 */

void ContentWidget::changeWebView(QWidget *view)
{
    qDebug() << "change view" << view;
    if(this->current_web_view != nullptr){
        this->ui->horizontalLayout->removeWidget(this->current_web_view);
        this->current_web_view->hide();
    }

    qDebug() << "add widget";
    // addWidget 似乎会自动将父组件设置为布局所在的组件
    this->ui->horizontalLayout->addWidget(view);
    qDebug() << "add widget finished";
    this->current_web_view = view;
    qDebug() << "view show" << this->current_web_view;
    this->current_web_view->show();
    qDebug() << "change view finished" << this->current_web_view->isVisible();
}

void ContentWidget::removeView(TabBarButton *view)
{
    if(current_web_view == view->view_widget){
        qDebug() << "remove widget is current widget";
        this->current_web_view = nullptr;
    }
    this->ui->horizontalLayout->removeWidget(view->view_widget);
    qDebug() << "remove" << view << view->view_widget;
    view->view_widget->deleteLater();
    view->deleteLater();
}

void ContentWidget::showConsoleWindow()
{
    qDebug() << "showConsoleWindow" << current_web_view;
    if(current_web_view == nullptr)return;
    CustomWebView *view = qobject_cast<CustomWebView*>(current_web_view);
    qDebug() << "current view: " << view;
    QWebEngineView *devView = new QWebEngineView();
    view->page()->setDevToolsPage(devView->page());
    devView->show();
}

